Python Programming for Beginners: A Step-by-Step Guide to Learn one of the Most Popular and Easy Programming Languages. Learn Basic Python Coding Fast with Examples and Tips by McKinnon Julian James
Author:McKinnon, Julian James [McKinnon, Julian James]
Language: eng
Format: epub
Published: 2020-02-08T16:00:00+00:00
The first method to handle the FileNotFoundError exception is to make sure that all the common reasons do not cause it.
Once you do, then you will need to choose the best way to handle the error, which is completely dependent on the reason you are opening a file in the first place.
Checking If File Exists
Again, there are always two ways to handle an exception: preventive and reactive.
The preventive method is to check if the file exists in the first place.
To do that, you will need to use the os (os.py) module that comes with your Python installation.
Then, you can use its path module’s isfile() function.
The path module’s file name depends on the operating system (posixpath for UNIX, ntpath for Windows, macpath for old MacOS).
For example:
>>> from os import path
>>> path.isfile("random.txt" )
False
>>> path.isfile("sampleFile.txt")
True
>>> _
Try and Except
You can also do it the hard way by using try, except, and else blocks.
>>> def openFile(filename):
try:
x = open(filename, "r")
except FileNotFoundError:
print("The file '" + filename + "' does not exist."
except FileNotFound:
print("The file '" + filename + "' does exist."
>>> openFile("random.txt")
The file 'random.txt' does not exist.
>>> openFile("sampleFile.txt")
The file 'sampleFile.txt' does exist.
>>> _
Creating a New File
If the file does not exist, and your goal is to overwrite any existing file anyway, then it will be best for you to use the "w" or "w+" access mode.
The access mode creates a new file for you if it does not exist.
For example:
>>> x = open("new.txt", "w")
>>> x.tell()
0
>>> _
If you are going to read and write, use "w+" access mode instead.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Coding Theory | Localization |
Logic | Object-Oriented Design |
Performance Optimization | Quality Control |
Reengineering | Robohelp |
Software Development | Software Reuse |
Structured Design | Testing |
Tools | UML |
Deep Learning with Python by François Chollet(12526)
Hello! Python by Anthony Briggs(9871)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9761)
The Mikado Method by Ola Ellnestam Daniel Brolund(9752)
Dependency Injection in .NET by Mark Seemann(9297)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8262)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7745)
Grails in Action by Glen Smith Peter Ledbrook(7671)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7521)
Becoming a Dynamics 365 Finance and Supply Chain Solution Architect by Brent Dawson(6759)
Microservices with Go by Alexander Shuiskov(6526)
Practical Design Patterns for Java Developers by Miroslav Wengner(6422)
Test Automation Engineering Handbook by Manikandan Sambamurthy(6401)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6383)
Angular Projects - Third Edition by Aristeidis Bampakos(5785)
The Art of Crafting User Stories by The Art of Crafting User Stories(5313)
NetSuite for Consultants - Second Edition by Peter Ries(5254)
Demystifying Cryptography with OpenSSL 3.0 by Alexei Khlebnikov(5075)
Kotlin in Action by Dmitry Jemerov(5023)
